import javax.naming.InitialContext;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

/**
 * 主界面
 */
public class MainFrame {
    private static boolean isReady = false;
    static writer MyWriter;
    static JFrame mainFrame = new JFrame();
    static JFrame LoginFrame = new JFrame();

    /**
     * 主
     * @param args
     */
    public static void main(String[] args){
        LoginFrameInit();
        SocketInit();
        //Init(mainFrame);
    }

    /**
     *Socket初始化
     */
    public static void SocketInit()  {
        try {
            Socket socket = new Socket("127.0.0.1", 8080);
            if (socket.isConnected()) {
                // 如果连接成功了就开启写和读的进程
                MyWriter = new writer(socket);
                MyWriter.send("Ready");
                new read(socket).start();
            } else {
                System.out.println("链接错误");
                System.exit(-1);
            }
        }catch (Exception e){
            new NoBorderDialog("服务端未开启");
            try {
                TimeUnit.SECONDS.sleep(1);
            }catch (InterruptedException exception){
                exception.printStackTrace();
            }
            System.out.println("connect failed");
            System.exit(-1);
        }

    }

    /**
     * 登录界面初始化
     */
    public static void LoginFrameInit(){
        LoginFrame.setTitle("Menu");
        LoginFrame.setLayout(null);
        LoginFrame.setSize(400,600);
        LoginFrame.setLocationRelativeTo(null);
        LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Button button = new Button("Start");
        button.setBounds(125,300,150,50);
        LoginFrame.add(button);
        LoginFrame.setVisible(true);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isReady){
                    LoginFrame.dispose();
                    Init(mainFrame);
                }else{
                    new NoBorderDialog("Wait for Other Player!");
                }

            }
        });

    }

    /**
     * 游戏界面初始化
     * @param frame 主界面
     */
    public static void Init(JFrame frame){
        //窗口大小
        frame.setSize(1600,1000);
        frame.setTitle("Domino");
        //居中
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        //关闭时关闭java虚拟机
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //所有骨牌实例初始化
        DominoManager.Instance.Init();
        //游戏区域
        GamePanel.Instance.Init();
        frame.add(GamePanel.Instance);
        frame.add(ScoreBoard.Instance);
        //回合提示
        WhoTurn.Instance.Init();
        frame.add(WhoTurn.Instance);
        RivalActionJlabel.Init();
        frame.add(RivalActionJlabel.RAction);
        //我方牌堆
        // MyLibrary.Instance.Init();
        // frame.add(MyLibrary.Instance);
        MyScrollLibrary.Instance.Init();
        frame.add(MyScrollLibrary.Instance);
        //对方牌堆
        RivalLibrary.Instance.Init();
        frame.add(RivalLibrary.Instance);
        //相关按钮
        TurnManager.Instance.Init();
        frame.add(TurnManager.Instance.Pass);
        frame.add(TurnManager.Instance.PlayACard);
        frame.add(TurnManager.Instance.PlatACardToTail);
        frame.add(TurnManager.Instance.RivalCount);
        //窗口可见
        frame.setVisible(true);
    }

    /**
     * 设置准备完成
     * @param flag 是否准备完成
     */
    public static void setIsReady(boolean flag){
            isReady = flag;
            System.out.println("准备完毕");
    }
}
